package org.cyzy.controller;

import org.cyzy.entity.ClassUser;
import org.cyzy.entity.CreateClass;
import org.cyzy.service.impl.ClassServceImpl;
import org.cyzy.service.insertClassUserService;
import org.cyzy.utils.MinioUtil;
import org.cyzy.utils.Random6;
import org.cyzy.utils.Uuid;
import org.cyzy.vo.CreateClassVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class ClassController {
    private static final Logger logger = LoggerFactory.getLogger(ClassController.class);

    @Autowired
    private ClassServceImpl classServce;

    @Autowired
    private Random6 random6;
    @Autowired
    private Uuid uuid;

    @Autowired
    private MinioUtil minioUtil;
    @Autowired
    private insertClassUserService insertClassUser;


//    @PostMapping("/insertClass")
//    public int insertClass(@RequestBody CreateClass createClass){
//        logger.info("接收到创建班级请求: {}", createClass);
//        // 生成班级id  班级码
////
////        createClass.setCoverImage(imageUrl);
//
//        createClass.setId(uuid.getUuid());
//        createClass.setClassCode(random6.getRandNum());
//        // 确保班级码不重复
//        while (classServce.selectClassCode(createClass.getClassCode()) > 0) {
//            // 如果班级码已存在，重新生成
//            createClass.setClassCode(random6.getRandNum());
//        }
//
//
//        return classServce.insertClass(createClass);
//    }


    @PostMapping("/insertClass")
    public int insertClass(@ModelAttribute CreateClassVo createClassVo){
        // 获取上传的图片文件
        MultipartFile file = createClassVo.getFile();
        System.out.println("file: " + file);

        String imageUrl = minioUtil.uploadImage(file);

        CreateClass createClass = new CreateClass(
               null,
                createClassVo.getName(),
                createClassVo.getSubject(),
                createClassVo.getGrade(),
                createClassVo.getDescription(),
                createClassVo.getSemester(),
                imageUrl,
                null,
                createClassVo.getTeacherId()
        );
                createClass.setId(uuid.getUuid());
        createClass.setClassCode(random6.getRandNum());
        ClassUser classUser = new ClassUser(
                createClass.getTeacherId(),
                createClass.getClassCode(),
                1,1
        );
        insertClassUser.insertClassUser(classUser);
        return classServce.insertClass(createClass);
    }




}